// 定于全局变量
var sfz_1_rotate = 0;
var sfz_2_rotate = 0;
var sfz_3_rotate = 0;
// 初始化执行执行
$(function(){
	// 为datagrid绑定双击事件
	$('#datagrid').datagrid({
		onDblClickCell: function(index,field,value){
			$('#datagrid').datagrid('selectRow',index);  //指定行选中
		    var currentRow = $("#datagrid").datagrid("getSelected");
		    showUserInfo(currentRow);
		}
	});
	
});

// 根据条件查询数据
function queryUserData(){
	var member = "";
	if($("#q_member").length!=0){
		member = $("#q_member").combobox('getValue');
	}
	//var code = $("#q_code option:selected").val();
	var code = "";
	if($("#q_code").length!=0){
		code = $("#q_code").combobox('getValue');
	}
	var status = $("#q_status").combobox('getValue');
	var startDate = $("#q_startDate").datetimebox('getValue');
	var endDate = $("#q_endDate").datetimebox('getValue');
	var name = $("#q_name").val();
	var number = $("#q_number").val();
//	alert("member:" + member);
//	alert("code:" + code);
//	alert("status:" + status);
//	alert("startDate:" + startDate);
//	alert("name:" + name);
//	alert("number:" + number);
//	return;
	
	$('#datagrid').datagrid('load',{
		"member": member,
		"status": status,
		"startDate": startDate,
		"endDate": endDate,
		"name": name,
		"number": number,
		"code": code
	});
}


// 删除用户信息
function deleteUserInfo(){
	var row = $('#datagrid').datagrid("getSelected");
	if(row == null){
		alert("请选择要删除的内容");
		return;
	}
	var status = row.status;
	if(status >= 2){
		alert("用户已经通过交易中心审核，不能删除！");
		return;
	}
	$.messager.confirm('提示框', '你确定要删除吗?',function(r){
		if(r){
			$.ajax({
				type:"post",
				url:"users/delete",
				data:{"id":row.id},
				dataType:"json",
				success:function(result){
					alert(result.msg);
					if(result.status == 1){
						var _index = $("#datagrid").datagrid("getRowIndex", row);
						$("#datagrid").datagrid("deleteRow", _index);
					}
				}
			});
		}
	});
	$("#datagrid").datagrid("unselectAll");
}


// 打开审核界面
function opneWindow(index){
	$("#datagrid").datagrid("selectRow", index);
	var row = $("#datagrid").datagrid("getSelected");
	$("#datagrid").datagrid("unselectAll");
	clearWindowData();
	showUserInfo(row);
}

/**
 * 根据datagrid获取到一行数据进行展示
 * @param {Object} row
 */
function showUserInfo(row){
	$("#id").val(row.id);
	$("#name").html(row.name);
	$("#age").html(row.age);
	$("#gender").html(row.sex); 
	$("#phone").html(row.phone);
	$("#email").html(row.email);
	$("#cid").html(row.cid);
	$("#address").html(row.address);
	$("#bank").html(row.bank);
	$("#card").html(row.card);
	$("#number").val(row.number);
	if(row.number != null){
		$("#number").attr("readonly",true)//将input元素设置为readonly
	}else{
		$("#number").attr("readonly",false)//将input元素设置为readonly
	}
	$("#province").html(row.province);

	document.getElementById("sfz_1").src = row.sfz1;
	document.getElementById("sfz_2").src = row.sfz2;
	document.getElementById("sfz_3").src = row.sfz3;
	$("#sfz_1").rotate({ 
	   bind: 
	     { 
	        click: function(){
	            sfz_1_rotate +=90;
	            $(this).rotate({ animateTo:sfz_1_rotate})
	        }
	     } 
	});
	$("#sfz_2").rotate({ 
	   bind: 
	     { 
	        click: function(){
	            sfz_2_rotate +=90;
	            $(this).rotate({ animateTo:sfz_2_rotate})
	        }
	     } 
	});
	$("#sfz_3").rotate({ 
	   bind: 
	     { 
	        click: function(){
	            sfz_3_rotate +=90;
	            $(this).rotate({ animateTo:sfz_3_rotate})
	        }
	     } 
	});
	
	$("#smd_id").val(row.id);
	$("#smd_sex").val(row.sex);
	$("#smd_name").val(row.name);
	$("#smd_phone").val(row.phone);
	$("#smd_member").val(row.member);
	$("#smd_email").val(row.email);
	$("#smd_number").val(row.number);
	
	$("#datagrid").datagrid("clearSelections"); // 清空表格被选中状态
	$('#win').window("setTitle", "审核【" + row.name + "】客户的资料");
	$('#win').window('maximize', true);
	$('#win').window('open');
}

function clostShenHeWindow(){
	clearWindowData();
	$('#win').dialog("close");
	
}

// 清空审核界面的数据
function clearWindowData(){
	$("#name").html("");
	$("#sex").html("");
	$("#age").html("");
	$("#phone").html("");
	$("#email").html("");
	$("#postcode").html("");
	$("#cid").html("");
	$("#address").html("");
	$("#bank").html("");
	$("#card").html("");
	$("#number").html("");
	$("#cidImgs").html("");
	$("#number").val("");
	$("#id").val("-1");
	document.getElementById("sfz_1").src = "";
	document.getElementById("sfz_2").src = "";
	document.getElementById("sfz_3").src = "";
	
	$("#smd_id").val("");
	$("#smd_sex").val("");
	$("#smd_name").val("");
	$("#smd_phone").val("");
	$("#smd_email").val("");
}

// 打开发送未通过消息的对话框
function openSendMsgDialog(){
	$("#sendMsgDialog").dialog("open");
}

// 提交未通过消息
function sendMsg(){
	var content = $("#textarea_msg").val();
	var name = $("#smd_name").val();
	var phone = $("#smd_phone").val();
	var sex = $("#smd_sex").val();
	var email = $("#smd_email").val();
	var member = $("#smd_member").val();
	var number = $("#smd_number").val();
	if(""==content || content == null){
		alert("请填写未通过的原因");
		return;
	}
	$.ajax({
		type:"post",
		url:"users/send/msg",
		data:{"id":$("#id").val(), "name":name, "member":member, "number":number, "phone":phone, "email":email,"sex":sex, "content":content},
		async:true,
		success:function(result){
			alert(result.msg);
			// 提示是否删除此用户数据
			if(result.status == 1){
				$.messager.confirm('提示框', '是否要删除此用户的注册信息',function(r){
					if(r){
						$.ajax({
							type:"post",
							url:"users/delete",
							data:{"id":$("#id").val()},
							dataType:"json",
							success:function(result){
								alert(result.msg);
								if(result.status == 1){
     									$("#datagrid").datagrid("reload");
								}
							}
						});
					}
				});
			}
			closeSsendMsgWindow();
		}
	});
}
// 关闭发送未通过消息对话框
function closeSsendMsgWindow(){
	$("#sendMsgDialog").dialog("close");
}




// 提交开出的交易账号
function submitNumber(){
	var number = $("#number").val();
	if(""==number || number == null){
		alert("请填写正确的交易账号");
		return;
	}
	$.ajax({
		type:"post",
		url:"users/number/create",
		data:{"id":$("#id").val(), "number":number},
		async:true,
		success:function(result){
			alert(result.msg);
			if(result.status ==1){
				$("#datagrid").datagrid("reload");
			}
			clostShenHeWindow();
		}
	});
}

// 交易中心审核通过，发送邮件通知客户
function approve(){
	$("#approve_btn").attr('disabled',"true");//添加disabled属性 
	$.ajax({
		type:"post",
		url:"users/approve",
		data:{"id":$("#id").val()},
		async:true,
		success:function(result){
			alert(result.msg);
			if(result.status ==1){
				$("#datagrid").datagrid("reload");
				$("#approve_btn").removeAttr("disabled"); //移除disabled属性 
			}
			clostShenHeWindow();
		}
	});
}

// 将注册账户标记为已激活
function activeNumber(id){
	$.ajax({
		type:"post",
		url:"users/number/active",
		data:{"id":id},
		async:true,
		success:function(result){
			alert(result.msg);
			if(result.status ==1){
				$("#datagrid").datagrid("reload");
				$("#approve_btn").removeAttr("disabled"); //移除disabled属性 
			}
			clostShenHeWindow();
		}
	});
}

